package com.sunni.controller;

import com.sunni.entity.Role;
import com.sunni.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Component("roleController")
@RequestMapping("/role")
public class RoleController {

    @Autowired
    @Qualifier("roleService")
    private RoleService roleService;
    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }

    /**
     * day06-11 : role-list.jsp 展示所有角色
     * @param modelAndView
     * @return modelAndView
     */
    @RequestMapping("/selectAll")
    public ModelAndView selectAll(ModelAndView modelAndView){
        // 存 数据
        modelAndView.addObject("roleList",roleService.selectAll());
        // 存 页面
        modelAndView.setViewName("role-list");  // 视图解析器 前缀pages 后缀.jsp

        return modelAndView;
    }

    /**
     * day06-11 : role-add.jsp 添加角色
     * @return 再去访问selectAll方法，重新查一遍然后跳回role-list.jsp
     */
    @RequestMapping("/add")
    public String add(Role role){
        roleService.add(role);
        return "redirect:/role/selectAll";
    }
}
